package com.stx.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stx.common.PageValue;
import com.stx.common.Result;
import com.stx.pojo.Judge;
import com.stx.pojo.Record;
import com.stx.pojo.Ruanyue;
import com.stx.service.IRuanyueService;
import com.stx.service.JudgeService;
import com.stx.service.RuanyueService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

@CrossOrigin
@RestController
@Tag(name = "软约相关接口")
@RequestMapping("/ruanyue")
public class RuanyueController {


    @Autowired
    private RuanyueService ruanyueService;
    @Autowired
    private IRuanyueService jruanyueService;

    @Operation(
            summary = "列表",
            description = "软约列表",
            tags = {"软约相关接口"}
    )
    @GetMapping("/list")
    public Result getRuanyue() {
        List<Ruanyue> list = jruanyueService.list();
        list.get(0).setTotal(list.size());
        return Result.success(list);
    }

    @Operation(
            summary = "添加/修改接口",
            description = "软约添加和修改列表",
            tags = {"软约相关接口"}
    )
    @PostMapping("/save")
    public Result save(@Valid @RequestBody Ruanyue ruanyue) {
        ruanyue.setRuaDate(LocalDateTime.now());
        if (ruanyue.getRuaState() == 1){
            boolean state = jruanyueService.findState(ruanyue);
            if (state){
                return Result.error("该状态已存在，请勿重复添加或者修改");
            }
        }
        jruanyueService.saveOrUpdate(ruanyue);
        return Result.success();
        /*if (ruanyue.getId() == null) {
            if (ruanyue.getRuaState() == 1){
                boolean state = jruanyueService.findState(ruanyue.getRuaState());
                System.out.println(state);
                if (state){
                    return Result.error("该状态已存在，请勿重复添加");
                }
            }
            jruanyueService.save(ruanyue);
        } else {
            if (ruanyue.getRuaState() == 1){
                boolean state = jruanyueService.findState(ruanyue.getRuaState());
                if (state){
                    return Result.error("该状态已存在，请勿修改");
                }
            }
            jruanyueService.updateById(ruanyue);
        }*/
    }

    @Operation(
            summary = "删除接口",
            description = "软约删除接口，根据id删除",
            tags = {"软约相关接口"}
    )
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        Ruanyue data = jruanyueService.getById(id);
        if (data.getRuaState() == 1){
            return Result.error("该活动正在进行中，请勿删除");
        }
        jruanyueService.removeById(id);
        return Result.success();
    }

    @Operation(
            summary = "获取最近软约",
            description = "获取最近软约",
            tags = {"软约相关接口"}
    )
    @PostMapping("/last")
    public Result getLastRuanyue() {
        return Result.success(ruanyueService.last());
    }

    @Operation(
            summary = "分页查询软约记录",
            description = "分页查询软约记录(期数/主题/地点)",
            tags = {"软约相关接口"}
    )
    @PostMapping("/page")
    public Result findPage(@Validated @RequestBody PageValue pageValue) {
        if (pageValue.getCurrentPage() != null && pageValue.getPageSize() != null) {
            Page<Ruanyue> page = ruanyueService.find(pageValue.getSubject(), pageValue.getAddress(), pageValue.getNumber(), pageValue.getCurrentPage(), pageValue.getPageSize());
            return Result.success(page);
        } else {
            List<Ruanyue> list = jruanyueService.list();
            return Result.success(list);
        }
    }
}
